


class Solution {
    using ll = long long;
public:
    long long countSubarrays(vector<int>& nums, int k) {
        int n = nums.size();
        int left = 0, cnt_max = 0;
        int mx = ranges::max(nums);
        ll ans = 0;
        for (int right = 0; right < n; ++right) {
            if (nums[right] == mx) ++cnt_max;
            while (cnt_max == k) {
                cnt_max -= nums[left] == mx;
                ++left;
            }
            ans += left;
        }
        return ans;
    }
};